package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.UserService;


/**
 * 账户设置
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class UserAction extends AppBaseAction{
	@Resource(name=UserService.BEAN_NAME)
	UserService userService;
	private String userId;
	private String shopId;
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	public String getShopId() {
		return shopId;
	}
	public void setShopId(String shopId) {
		this.shopId = shopId;
	}
	/**
	 * 获取个人的统计信息(
	 * 		积分：5620 
			待收货订单：1份 
			待付款订单：2份 
			已成功订单：)
	 * @return
	 */
	public String getStatInfo(){
		JSONObject jsonObject = null;
		try{
			String userId = this.getUserId();
			jsonObject = userService.getStatInfo(userId);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 获取用户基本信息
	 * @return
	 */
	public String getUserInfo(){
		JSONObject jsonObject = null;
		try{
			this.validateLoginApp();
			this.validateSign(getUserId(),getLoginSign());
			jsonObject = userService.getUserInfo(getUserId());
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 更新用户信息
	 * @return
	 */
	public String updateUserInfo(){
		JSONObject jsonObject = null;
		try{
			/*this.request.setCharacterEncoding("UTF-8"); // 设置处理请求参数的编码格式
			InputStream is = this.request.getInputStream();//获取从客户端上传的数据流
			response.setContentType("multipart/form-data;charset=UTF-8"); // 设置Content-Type字段值   
			String uploadPath = "D:\\app\\member\\";
			File file = new File(uploadPath);
			 if (!file.exists())  
			 {
			 file.mkdir();  
			 }
			// 上传文件保存到本地的文件名  
			 String filename = request.getParameter("filename"); 
			  FileOutputStream fos = new FileOutputStream(filename); 
			// 读取数据流大小
			  byte[] buffer = new byte[is.available()];   
			  int count = 0;  
			  // 开始读取上传文件的字节，并将其输出到服务端的上传文件输出流中   
			  while ((count = is.read(buffer)) > 0)  
			  {  
			  fos.write(buffer, 0, count); // 向服务端文件写入字节流   
			  }  
			fos.close(); // 关闭FileOutputStream对象   
			is.close(); // InputStream对象   
*/			
			String userId = this.getUserId();
			//String userName = this.request.getParameter("userName");
			//String mobile = this.request.getParameter("mobile");
			String sex = this.request.getParameter("sex");
			String birthday = this.request.getParameter("birthday");//生日
			String phoneNumber = this.request.getParameter("phoneNumber");
			//String email = this.request.getParameter("email");
			String address = this.request.getParameter("address");//所在地
			//String zipCode = this.request.getParameter("zipcode");
			//String realName = this.request.getParameter("realName");
			String iconUrl = this.request.getParameter("iconUrl");
			String nickName = this.request.getParameter("nickName");//昵称
			String realName = this.request.getParameter("realName");//真实姓名
			
			this.validateLoginApp();
			this.validateSign(getUserId(),phoneNumber,sex,birthday,nickName,address,iconUrl,realName);
			jsonObject = userService.appUpdateUserInfo(userId,phoneNumber,sex,birthday,nickName,address,iconUrl,realName);
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "更新用户信息成功，来源:mobile");//记录操作日志

		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "更新用户信息失败，原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
	}
	/*
	 * 邀请用户注册
	 */
	public String inviteUser(){
		String tempUserId = this.request.getParameter("userId");
		if(tempUserId != null){
			userId = tempUserId;
			shopId = "";
		}
		return "/reg/reg.jsp";
	}
	
}
